package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dao.PmsShopDao;
import com.macro.mall.dto.ShopmsShopParam;
import com.macro.mall.model.PmsShop;
import com.macro.mall.service.PmsShopService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 店铺信息表(PmsShop)表服务实现类
 *
 * @author liu
 * @since 2021-04-21
 */
@Service("pmsShopService")
public class PmsShopServiceImpl implements PmsShopService {
    @Resource
    private PmsShopDao pmsShopDao;

    /**
     * 根据主键id查询单个店铺信息
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public PmsShop queryById(Long id) {
        return this.pmsShopDao.queryById(id);
    }

    @Override
    public PmsShop queryByUserName(String userName) {
        PmsShop p = pmsShopDao.queryByUserName(userName);
        System.out.println(p);
        return p;
    }

    /**
     * 分页查询所有店铺信息
     *
     * @param pageNum 当前第几页
     * @param pageSize 一页有多少记录
     * @return 所有店铺信息list
     */
    public List selectAll(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        Integer beginRow = pageSize;
        Integer endRow = pageSize * (pageNum-1);
        List<PmsShop> p = pmsShopDao.selectAll(beginRow, endRow);
        return p;
    }

    /**
     * 新增店铺
     *
     * @param param 店铺对象
     * @return 成功与否
     */
    @Override
    public int insert(ShopmsShopParam param) {
        int count;
        PmsShop pmsShop = new PmsShop();
        BeanUtils.copyProperties(param, pmsShop);
        pmsShop.setUmsUsername(param.getUsername());
        this.pmsShopDao.insert(pmsShop);
        count=1;
        return count;
    }

    /**
     * 修改店铺信息
     *
     * @param pmsShop 店铺对象
     * @return 成功与否
     */
    @Override
    public int update(PmsShop pmsShop) {
        int count;
        this.pmsShopDao.update(pmsShop);
        count=1;
        return count;
    }

    /**
     * 删除店铺（通过主键id）
     *
     * @param id 主键id
     * @return 成功与否
     */
    @Override
    public boolean deleteById(Long id) {
        return this.pmsShopDao.deleteById(id) > 0;
    }

    /**
     * 批量删除店铺（店铺id）
     *
     * @param shopIds 店铺id list
     * @return 成功与否
     */
    @Override
    public int deleteShops(List<Long> shopIds) {
        int count;
        for(int i=0;i < shopIds.size();i++){
            Long shopId =shopIds.get(i);
            pmsShopDao.deleteByShopId(shopId);
        }
        count=1;
        return count;
    }

    /**
     * 组合查询
     *
     * @param pageNum 当前第几页
     * @param pageSize 一页有多少记录
     * @param shopName 店铺名字模糊查询
     * @param showStatus 店铺状态0->不显示；1->显示
     * @return 查询结果列表
     */
    @Override
    public List selectByNameAndStatus(Integer pageSize, Integer pageNum, String shopName, Integer showStatus) {
        Integer beginRow = pageSize;
        Integer endRow = pageSize * (pageNum-1);
        if(shopName.isEmpty()){
            if(showStatus==-1){//店名空状态空
                return pmsShopDao.selectAll(beginRow, endRow);
            }else{//店名空状态不空
                return pmsShopDao.selectByStatus(beginRow, endRow,showStatus);
            }
        }else {
            if(showStatus==-1){//店名不空状态空
                return pmsShopDao.selectByName(beginRow, endRow,shopName);
            }else{//店名与状态都不为空
                return pmsShopDao.selectByNameAndStatus(beginRow, endRow,shopName,showStatus);
            }
        }
    }


}
